/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                           *
 *                          Copyright (c) 2010                               *
 *                  Alen Stojanov [ ioi.alen@gmail.com ]                     *
 *                Jacobs University Bremen, Bremen, Germany                  *
 *                                                                           *
 *                          All Rights Reserved.                             *
 *                                                                           *
 *    Permission to use, copy, modify and distribute this software and       *
 *    its documentation is hereby granted, provided that both the            *
 *    copyright notice and this permission notice appear in all copies of    *
 *    the software, derivative works or modified versions, and any           *
 *    portions thereof, and that both notices appear in supporting           *
 *    documentation.                                                         *
 *                                                                           *
 *    This code is distributed in the hope that it will be useful, but       *
 *    WITHOUT ANY WARRANTY; without even the implied warranty of             *
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS       *
 *    DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER          *
 *    RESULTING FROM THE USE OF THIS SOFTWARE.                               *
 *                                                                           *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "tsthread.h"

TSThread::TSThread()
{
	this->progressBar 		= NULL;
	this->lblProgress		= NULL;

	this->complete_double 	= 0;
	this->complete_qint64 	= 0;
	this->partial_double 	= 0;
	this->partial_qint64 	= 0;
}

TSThread::TSThread(QProgressBar* progressBar, QLabel* lblProgress)
{
	this->complete_double 	= 0;
	this->complete_qint64 	= 0;
	this->partial_double 	= 0;
	this->partial_qint64 	= 0;

	setProgressBar(progressBar);
	setLabel(lblProgress);
}

void TSThread::setLabel(QLabel* lblProgress)
{
	this->lblProgress = lblProgress;
	connect(this, SIGNAL(setText(const QString &)), lblProgress, SLOT(setText(const QString &)));
}

void TSThread::setProgressBar(QProgressBar* pBar)
{
	progressBar = pBar;
	connect(this, SIGNAL(setValue(int)), progressBar, SLOT(setValue(int)));
}

void TSThread::update(qint64 progress)
{
	int percent = 100;

	if ( (progress + partial_qint64) <= complete_qint64 ) {
		percent = (progress + partial_qint64) * 100 / complete_qint64;
	}

	if ( progressBar->value() != percent ) {
		emit setValue(percent);
	}
}

void TSThread::update(double progress)
{
	int percent = 100;

	if ( (progress + partial_double) <= complete_double ) {
		percent = round( (progress + partial_double) * 100 / complete_double );
	}

	if ( progressBar->value() != percent ) {
		emit setValue(percent);
	}
}

void TSThread::updateText(QString text)
{
	emit setText(text);
}

void TSThread::run()
{
	// Do nothing
}

TSThread::~TSThread()
{
	// Do nothing
}
